package cn.xzq.crm.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.xzq.crm.entity.Customer;
import cn.xzq.crm.entity.User;
import cn.xzq.crm.entity.Visit;
import cn.xzq.crm.execption.VisitException;
import cn.xzq.crm.service.CustomerService;
import cn.xzq.crm.service.UserService;
import cn.xzq.crm.service.VisitService;

@Controller("visitAction")
public class VisitAction extends ActionSupport implements ModelDriven<Visit>{
	//注入依赖
	@Autowired
	private VisitService visitService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private UserService userService;
	//模型驱动
	private Visit visit = new Visit();
	@Override
	public Visit getModel() {
		return visit;
	}
	public Visit getVisit() {
		return visit;
	}

	//值栈的使用
	List<User> userList = new ArrayList<User>();
	List<Visit> visitList = new ArrayList<Visit>();
	List<Customer> customerList = new ArrayList<Customer>();
	public List<User> getUserList() {
		return userList;
	}
	public List<Customer> getCustomerList() {
		return customerList;
	}
	public List<Visit> getVisitList() {
		return visitList;
	}
	
	
	//================================================
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String toAddPage(){
		//得到用户列表
		userList = userService.findAll();
		//得到客户列表
		customerList = customerService.findAll();
		return "toAddPage";
	}
	
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String toEditPage(){
		//得到用户列表
		userList = userService.findAll();
		//得到客户列表
		customerList = customerService.findAll();
		//得到visit的id，获取对应的visit对象
		Visit f_visit= visitService.findOneById(visit.getVid());
		if(f_visit == null){
			return "error";
		}
		visit = f_visit;//给值栈赋值
		return "toEditPage";
	}
	
	/**
	 * 跳转到列表页面
	 * @return
	 */
	public String toListPage(){
		visitList= visitService.findAll();
		return "toListPage";
	}
	
	/**
	 * 跳转到综合查询页面
	 * @return
	 */
	public String toQueryPage(){
		//得到用户列表
		userList = userService.findAll();
		//得到客户列表
		customerList = customerService.findAll();
		return "toQueryPage";
	}
	
	/**
	 * 添加客户拜访记录
	 * @return
	 */
	public String addVisit(){
		/*System.out.println(visit.getCustomer());
		System.out.println(visit.getUser());*/
		try {
			visitService.add(visit);
		} catch (VisitException e) {
			return "error";
		}
		return "success";
	}
	
	/**
	 * 添加客户拜访记录
	 * @return
	 */
	public String update(){
		try {
			visitService.update(visit);
		} catch (VisitException e) {
			return "error";
		}
		return "success";
	}
	
	/**
	 * 多条件综合查询
	 * @return
	 */
	public String moreCondition(){
		//得到用户列表
		userList = userService.findAll();
		//得到客户列表
		customerList = customerService.findAll();
		//封装visit完整数据
		Customer customer = customerService.findOneById(visit.getCustomer().getCustId());
		visit.setCustomer(customer);
		User user = userService.findOneById(visit.getUser().getUid());
		visit.setUser(user);
		//调用serivce方法
		visitList = visitService.findMoreCondition(visit);
		
		return "moreCondition";
	}
	
	
	
}
